/**
 * 文 件 名:  ContentController.java
 * 版      权:  财之道  Copyright ©2011-2015 All Rights Reserved
 * 描      述:  
 */
package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.czd.support.model.User;
import com.czd.support.service.UserService;
import com.czd.support.util.MD5Utils;

/**
 * @Description TODO
 * @Author JinMa.Xiao
 * @Date 2015年6月25日 上午11:59:18
 * @Version v1.0
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@Autowired
	private UserService useService;

	/** 
	 * 增加用户.跳转页面
	 */
	@RequestMapping(value = { "/", "", "add" }, method = RequestMethod.GET)
	public String add() {
		return "user/add";
	}

	/**
	 * 增加用户
	 */
	@RequestMapping(value = { "/", "", "addUser" }, method = RequestMethod.POST)
	public String addUser(HttpServletRequest request, Model model, User user) {
		String str2Md5 = MD5Utils.str2Md5(user.getPassword());
		user.setPassword(str2Md5);
		try {
		if (useService.insert(user)) {
		User  user2=	useService.findByUser(user);
			model.addAttribute("user", user2);
			return "support/details";
		}
		} catch (Exception e) {
			return error(model, "操作失败", "录入内容信息失败,可能原因手机号码,邮箱,用户名已经注册");
		}
		return error(model, "操作失败", "录入内容信息失败");
	}
	

	/**
	 *@PathVariable String mobilePhone
	 */
	@ResponseBody
	@RequestMapping(value = {"phone" }, method = RequestMethod.GET)
	public Boolean remostePhone(HttpServletRequest request, Model model,User  user) {
		String mobilePhone = request.getParameter("mobilePhone");
		String mail = request.getParameter("mail");
		if(mobilePhone!=null ){
			user.setMobilePhone(mobilePhone);
			List<User> findByPhone = useService.findByPhone(user);
			if(findByPhone.size()==0){//不存在  说明没有注册过   为"
				return  true ;
			} else {
				return   false ;   
			}
		}
		if(mail!=null ){
			user.setMail(mail);
			List<User> findByMail = useService.findByMail(user);
			if(findByMail.size()==0){//不存在  说明没有注册过   为"
				return  true ;
			} else {
				return   false ;   
			}
		}
		return false  ;
}
}